/**
 * 
 */
package org.mhz.ootw.util;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.solr.client.solrj.response.FacetField;
import org.mhz.ootw.tree.Data;
import org.mhz.ootw.tree.JsTreeNode;

/**
 * @author Mahan
 *
 * 
 */
public final class JsTreeHelper
{
    /**
     * Private constructor
     */
    private JsTreeHelper()
    {
        
    }
    
    /**
     * Get the tree node if it exists
     *
     * @param aTreeNodes - List of tree nodes to search
     * @param aNodeTitle - The title of the node to find
     * @return The returned node if found in the list
     */
    public static JsTreeNode getNodeIfExists(final List<JsTreeNode> aTreeNodes,
                                             final String aNodeTitle)
    {
        if (CollectionUtils.isNotEmpty(aTreeNodes))
        {
            for (JsTreeNode treeNode : aTreeNodes)
            {
                final Data treeNodeData = treeNode.getData();
                
                if ((treeNodeData != null) && (treeNodeData.getTitle() != null))
                {
                    if (treeNodeData.getTitle().equalsIgnoreCase(aNodeTitle))
                    {
                        return treeNode;
                    }
                }
            } 
        }
        
        return null;
    }
    
    /**
     * Set the facet field count
     *
     * @param aFacetFieldsList - The list facet fields
     * @param aFacetField - The facet field
     * @param aFieldToSet - The field to set  
     * @return The returned field
     */
    public static String setFacetFieldCount(final List<FacetField> aFacetFieldsList,
                                            final String aFacetField,
                                            final String aFieldToSet)
    {
        String field = aFieldToSet;
        
        if (CollectionUtils.isNotEmpty(aFacetFieldsList))
        {
            OUTERMOST:
            for (FacetField facetField : aFacetFieldsList)
            {
                if (facetField != null)
                {
                    final List<FacetField.Count> facetFieldValues = facetField.getValues();
                    
                    if (CollectionUtils.isNotEmpty(facetFieldValues))
                    {
                        for (FacetField.Count facetFieldCount : facetFieldValues)
                        {
                            if (facetFieldCount != null)
                            {
                                final String name = facetFieldCount.getName();
                                final long count = facetFieldCount.getCount();
                                
                                if (name.equalsIgnoreCase(aFacetField))
                                {
                                    field += " (" + count + ")";
                                    break OUTERMOST;
                                }                                            
                            }
                        }
                    }
                }
            }
        } 
        
        return field;
    }
}
